package cn.lanqiao.bigDate.service.sys.impl;


import cn.lanqiao.bigDate.entity.sys.SysArea;
import cn.lanqiao.bigDate.entity.sys.SysRole;
import cn.lanqiao.bigDate.mapper.sys.SysAreaMapper;
import cn.lanqiao.bigDate.service.sys.SysAreaService;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;

@Service
public class SysAreaServiceImpl implements SysAreaService {

    @Resource
    private SysAreaMapper sysAreaMapper;


    /**
     * 分页查询
     * @param page  分页
     * @param
     * @return
     */
    @Override
    public IPage<SysArea> findByPage(Page<SysArea> page, String str) {
        QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        JSONObject p = JSONObject.parseObject(str);

        if(!p.get("levelLike").equals(""))
            queryWrapper.like("level", p.get("levelLike")).or();

        if (!p.get("areaCodeLike").equals(""))
            queryWrapper.like("area_code", p.get("areaCodeLike")).or();

        if (!p.get("nameLike").equals(""))
            queryWrapper.like("name",p.get("nameLike")).or();

          Page<SysArea> sysAreaPage = sysAreaMapper.selectPage(page, queryWrapper);
          return sysAreaPage;
    }

    /**
     * 根据id查询区域
     * @param id
     * @return
     */
    @Override
    public SysArea findSysAreaOne(String id) {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysArea sysArea = sysAreaMapper.selectOne(wrapper);
        return sysArea;
    }

    /**
     * 修改区域信息
     * @param sysArea
     * @return
     */
    @Override
    public Boolean updateSysArea(SysArea sysArea) {
        if (sysArea !=null && sysArea.getId()!=null){
            String id = sysArea.getId();
            //查询源对象
            SysArea sysAreaOne = findSysAreaOne(id);
            sysAreaOne.setLevel(sysArea.getLevel());
            sysAreaOne.setParentCode(sysArea.getParentCode());
            sysAreaOne.setAreaCode(sysArea.getAreaCode());
            sysAreaOne.setZipCode(sysArea.getZipCode());
            sysAreaOne.setCityCode(sysArea.getCityCode());
            sysAreaOne.setName(sysArea.getName());
            sysAreaOne.setShortName(sysArea.getShortName());
            sysAreaOne.setMergerName(sysArea.getMergerName());
            sysAreaOne.setPinyin(sysArea.getPinyin());
            sysAreaOne.setLng(sysArea.getLng());
            sysAreaOne.setLat(sysArea.getLat());
            int i = sysAreaMapper.updateById(sysAreaOne);
            if (i==1){
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     *添加区域
     * @param sysArea
     * @return
     */
    @Override
    public Boolean saveSysArea(SysArea sysArea) {
        int insert = sysAreaMapper.insert(sysArea);
        if (insert == 1){
            return true;
        }
        return false;
    }

    /**
     * 删除区域信息
     * @param ids 格式  id1,id2,id3
     * @return
     */
    @Override
    public Boolean deleteSysArea(String ids) {
        //判断字符串是否为空
        if (!StringUtils.isEmpty(ids)){
            String[] id = ids.split(",");
            List<String> strings = Arrays.asList(id);
            int i = sysAreaMapper.deleteBatchIds(strings);
            if (i>0){
                return true;
            }
        }
        return false;
    }

    /**
     * 查询所有数据
     * @return
     */
    @Override
    public List<SysArea> findAll() {
        QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>();
        sysAreaQueryWrapper.orderByAsc("id");
        List<SysArea> sysAreas = sysAreaMapper.selectList(null);
        return sysAreas;

    }

    @Override
    public void addArea(SysArea sysArea) {
        saveSysArea(sysArea);
    }

}
